﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Winner.Game.Entity.DTOModel;
using Winner.Game.Entity.POCOModel;
using Winner.Game.Entity.ViewModel;
using Winner.Game.Services;

namespace Winner.Game.Web.Controllers
{
    public class UserInfoController : Controller
    {
        private UserLevelService _userLevelService = new UserLevelService();
        private UserInfoServices _userInfoService = new UserInfoServices();

        public ActionResult Index()
        {
            //获得会员等级下拉框所需的数据源
            ViewBag.UserLevelList = GetUserLevelList();

            return View();
        }

        /// <summary>
        /// 会员的下拉框
        /// </summary>
        /// <returns></returns>
        private List<SelectListItem> GetUserLevelList()
        {
            using (var db = new GamePrintModel())
            {
                //查询用户等级的信息
                //下拉框
                var userLevelList = _userLevelService.GetList(c => true).Select(c => new SelectListItem()
                {
                    Text = c.LevelName,
                    Value = c.LevelID.ToString()
                }).ToList();

                userLevelList.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });

                return userLevelList;


                //var UserlevelList = db.UserLevel.Where(c => true).Select(c => new SelectListItem() { Text = c.LevelName, Value = c.LevelID.ToString() }).ToList();
                //UserlevelList.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });
                //return UserlevelList;
            }
        }


        [HttpPost]
        public ActionResult UserInfoList(UserInfoSearchViewModel viewModel)
        {
            //调用业务逻辑层的分页方法
            var result = _userInfoService.UserInfoList(viewModel);

            //OperateResult中的Data属性包含了分页的数据
            return Json(result.Data);
        }

        /// <summary>
        /// 页面实现数据添加
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            //用户等级下拉框绑定
            ViewBag.UserLevelList = GetUserLevelList();

            return View("Edit");
        }

        [HttpPost]
        public ActionResult Add(UserInfoEditViewModel viewModel)
        {
            var result = _userInfoService.AddUserInfo(viewModel);
            return Json(result);
        }


        /// <summary>
        /// 页面实现数据修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit(int id)
        {
            //用户等级下拉框绑定
            ViewBag.UserLevelList = GetUserLevelList();

            var result = _userInfoService.GetUserInfoID(id);
            if (result.State == ResultState.Success)
            {
                return View(result.Data);
            }
            else
            {
                return HttpNotFound();
            }
        }

        [HttpPost]
        public ActionResult Edit(UserInfoEditViewModel viewModel)
        {
            var result = _userInfoService.EditUserInfo(viewModel);
            return Json(result);
        }


        /// <summary>
        /// 页面实现数据删除
        /// </summary>
        /// <param name="deleteId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int deleteId)
        {
            var result = _userInfoService.DeleteUserInfo(deleteId);
            return Json(result);
            
        }
    }
}